<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetTreeDataApi} from "@/api";
import type {Tree} from "@/interface";
import 'echarts-wordcloud'

const tree = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const treeData = ref<Tree>({
  name:'',
  children:[]
})
const init = async () => {
  const {data:res} = await useGetTreeDataApi()
  treeData.value = res
  myChart.value = markRaw(echarts.init(tree.value));
  draw();
}
const draw = () => {
  option.value = {
    tooltip: {
      trigger: 'item',
      triggerOn: 'mousemove'
    },
    series: [
      {
        type: 'tree',
        data: [treeData.value],
        top: '1%',
        left: '7%',
        bottom: '1%',
        right: '20%',
        symbolSize: 7,
        label: {
          position: 'left',
          verticalAlign: 'middle',
          align: 'right',
          fontSize: 9
        },
        leaves: {
          label: {
            position: 'right',
            verticalAlign: 'middle',
            align: 'left'
          }
        },
        emphasis: {
          focus: 'descendant'
        },
        expandAndCollapse: true,
        animationDuration: 550,
        animationDurationUpdate: 750
      }
    ]
  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="tree" class="tree">
  </div>
</template>

<style scoped>
.tree{
  width: 100%;
  height: 100%;
}
</style>